package com.ruoyi.web.controller.comlbs;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.AlarmType;
import com.ruoyi.system.domain.UserAlarmType;
import com.ruoyi.system.domain.request.alarmtype.AlarmTypeExistParam;
import com.ruoyi.system.domain.request.alarmtype.AlarmTypeLevelSettingParam;
import com.ruoyi.system.service.IAlarmTypeService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.IUserAlarmTypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 告警类型Controller
 * 
 * @author wp
 * @date 2023-08-10
 */
@RestController
@RequestMapping("/comlbs/alarmType")
public class AlarmTypeController extends BaseController {

    @Autowired
    private IAlarmTypeService alarmTypeService;
    @Autowired
    private IUserAlarmTypeService userAlarmTypeService;

    @ApiOperation("告警类型列表")
    @ApiOperationSupport(includeParameters = {"name","level","source", "names"})
    @GetMapping("/list")
    public TableDataInfo list(AlarmType alarmType) {
        startPage();
        List<AlarmType> list = alarmTypeService.selectList(alarmType);
        return getDataTable(list);
    }

    @ApiOperation("告警类型列表(不分页)")
    @ApiOperationSupport(includeParameters = {"name","level","source", "names"})
    @GetMapping("/listAll")
    public AjaxResult listAll(AlarmType alarmType) {
        return success(alarmTypeService.selectList(alarmType));
    }

    /**
     * 获取告警类型详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(alarmTypeService.getById(id));
    }

    @ApiOperation("新增告警类型")
    @ApiOperationSupport(includeParameters = {"name","type","level","source","remark"})
    @Log(title = "告警类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AlarmType alarmType) {
        checkExistName(new AlarmTypeExistParam(null, alarmType.getName(), alarmType.getType()));
        return toAjax(alarmTypeService.save(alarmType));
    }

    @ApiOperation("编辑告警类型")
    @ApiOperationSupport(includeParameters = {"id","name","type","level","source","remark"})
    @Log(title = "告警类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AlarmType alarmType) {
        checkExistName(new AlarmTypeExistParam(alarmType.getId(), alarmType.getName(), alarmType.getType()));
        return toAjax(alarmTypeService.updateById(alarmType));
    }

    /**
     * 删除告警类型
     */
    @Log(title = "告警类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        userAlarmTypeService.removeByTypeIds(ids);
        alarmTypeService.removeByIds(Arrays.asList(ids));
        return toAjax(true);
    }

    @ApiOperation("告警名称校验")
    @PostMapping("/checkExistName")
    public AjaxResult checkExistName(@Validated AlarmTypeExistParam param) {
        alarmTypeService.checkExistName(param);
        return success();
    }

    @ApiOperation("告警级别设置")
    @PostMapping("/levelSetting")
    public AjaxResult levelSetting(@Validated AlarmTypeLevelSettingParam param) {
        alarmTypeService.levelSetting(param);
        return success();
    }
}
